package pswvclass.pswvandroid2.com.user;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pswvclass.pswvandroid2.R;
import pswvclass.pswvandroid2.com.kwords.KwordAdd;
import pswvclass.pswvandroid2.com.kwords.KwordList;
import pswvclass.pswvandroid2.com.po.KWords;
import pswvclass.pswvandroid2.com.util.DBHelper;
import pswvclass.pswvandroid2.com.util.HttpUtils;

/**
 * Created by Administrator on 2018/11/1.
 */
public class UserPerfect extends Fragment implements View.OnClickListener {
    private String tag = "UserPerfect";
    private TextView user_degree;
    private TextView user_sex;
    private TextView user_interest;
    private TextView user_strong_point;
    private Button user_click;
    private static int status;
    private String kwords;
    private String content;
    protected static final int SUCCESS = 1;
    protected static final int ERROR = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.pswv_user_perfect, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            status = bundle.getInt("status");
            kwords = bundle.getString("kwords");
            content = bundle.getString("content");
        }
        user_degree = (EditText) getView().findViewById(R.id.user_degree);
        user_sex = (EditText) getView().findViewById(R.id.user_sex);
        user_interest = (EditText) getView().findViewById(R.id.user_interest);
        user_strong_point = (EditText) getView().findViewById(R.id.user_strong_point);
        user_click = (Button) getView().findViewById(R.id.user_click);
        user_click.setOnClickListener(this);
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case SUCCESS: {
                    if (status == 0) {
                        KwordAdd ka = new KwordAdd();
                        Bundle bundle = new Bundle();
                        bundle.putString("kwords", kwords);
                        bundle.putString("content", content);
                        ka.setArguments(bundle);
                        getFragmentManager()
                                .beginTransaction()
                                .addToBackStack(null)
                                .replace(R.id.realtabcontent, ka).commit();
                        Toast.makeText(getContext(), "已完善", Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
                case ERROR: {
                    Toast.makeText(getContext(), "添加成功", Toast.LENGTH_SHORT).show();
                    break;
                }
                default: {
                    break;
                }
            }
        }

        ;
    };

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.user_click: {
                final String degree = user_degree.getText().toString().trim();
                final String sex = user_sex.getText().toString().trim();
                final String interest = user_interest.getText().toString().trim();
                final String strongPoint = user_strong_point.getText().toString().trim();

                if (TextUtils.isEmpty(degree) || TextUtils.isEmpty(sex) || TextUtils.isEmpty(interest) || TextUtils.isEmpty(strongPoint)) {
                    Toast.makeText(getContext(), "不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                DBHelper db = new DBHelper(getContext());
                SQLiteDatabase sd = db.getWritableDatabase();
                int userId = 0;
                Cursor cursor = sd.query("user", new String[]{"userId"}, "state=?", new String[]{"1"}, null, null, null);
                while (cursor.moveToNext()) {
                    userId = cursor.getInt(0);
                }
                cursor.close();
                final int finalUserId = userId;

                new Thread() {
                    public void run() {
                        try {
                            HttpUtils hu = new HttpUtils();
                            String data2;
                            final String src = getResources().getString(R.string.src_mysql) + "pswv/api/userAction/userPerfect";
                            final Map<String, String> map1 = new HashMap<String, String>();
                            map1.put("id", finalUserId + "");
                            map1.put("degree", degree + "");
                            map1.put("sex", sex + "");
                            map1.put("interest", interest + "");
                            map1.put("strongPoint", strongPoint + "");
                            data2 = hu.submitPostData(src, map1, "utf-8");
                            JSONObject jsonObj = new JSONObject(data2);
                            int result = Integer.parseInt(jsonObj.getString("code"));
                            if (result == 0) {
                                Message msg = Message.obtain();
                                msg.what = SUCCESS;
                                mHandler.sendMessage(msg);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            Message msg = Message.obtain();
                            msg.what = ERROR;
                            mHandler.sendMessage(msg);
                            Log.v(tag, e + "");
                        }
                    }
                }.start();
            }
            break;
            default:
                break;
        }
    }


}
